using MapsterMapper;
using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.Core;
using EasyCaching.Core;
using AiQiuQuan.Sport.WebApi.Entities;
using AiQiuQuan.Sport.WebApi.Repositories;
using Microsoft.EntityFrameworkCore;
using AiQiuQuan.Sport.WebApi.Core;

namespace AiQiuQuan.Sport.WebApi.Services.Impl
{
    /// <summary>
    /// 俱乐部管理
    /// </summary>
    public partial class ClubService : IClubService
    {
        private readonly IMapper _mapper;
        private readonly IEasyCachingProvider _cache;
        private readonly ILogger<ClubService> _logger;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IClubRepository _clubRepository;
        private readonly ICurrentInfo _currentInfo;
        private readonly IVenueInfoRepository _venueInfoRepository;
        private readonly IActivityRepository _activityRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public ClubService(
            IMapper mapper,
            IEasyCachingProvider cache,
            ILogger<ClubService> logger,
            IUnitOfWorkManager unitOfWorkManager,
            IClubRepository clubRepository,
            ICurrentInfo currentInfo,
            IVenueInfoRepository venueInfoRepository,
            IActivityRepository activityRepository)
        {
            _mapper = mapper;
            _logger = logger;
            _cache = cache;
            _unitOfWorkManager = unitOfWorkManager;
            _clubRepository = clubRepository;
            _currentInfo = currentInfo;
            _venueInfoRepository = venueInfoRepository;
            _activityRepository = activityRepository;
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        public async Task<ClubDto> GetAsync(Guid id)
        {
            var club = await _clubRepository.FirstOrDefaultAsync(m => m.ID == id);
            var result = _mapper.Map<Club, ClubDto>(club);
            await SetVenueInfoAsync(result, 0, 0);
            return result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        public async Task<UnaryResult> AddAsync(ClubAddDto model)
        {
            var club = _mapper.Map<Club>(model);
            club.CreatorId = _currentInfo.UserId;
            await _clubRepository.AddAsync(club);

            return UnaryResult.Succeed();
        }

        /// <summary>
        /// 修改
        /// </summary>
        public async Task<UnaryResult> UpdateAsync(ClubUpdateDto model)
        {
            var club = await _clubRepository.FirstOrDefaultAsync(m => m.ID == model.ID);
            if (club == null)
            {
                return UnaryResult.Faild(10, "俱乐部不存在");
            }

            _mapper.Map(model, club);
            await _clubRepository.UpdateAsync(club);

            return UnaryResult.Succeed();
        }

        /// <summary>
        /// 删除
        /// </summary>
        public async Task<UnaryResult> DeleteAsync(Guid id)
        {
            await _clubRepository.DeleteAsync(p => p.ID == id);
            return UnaryResult.Succeed();
        }

        /// <summary>
        /// 俱乐部活动新增
        /// </summary>
        public async Task<UnaryResult> AddActivityAsync(ClubActivityAddDto model)
        {
            var activity = await CreateVenueActivityAsync(model);
            await _activityRepository.AddAsync(activity);
            return UnaryResult.Succeed();
        }

        /// <summary>
        /// 俱乐部活动分页列表
        /// </summary>
        public async Task<PageResult<ClubActivityDto>> GetClubActivityPageListAsync(ClubActivityPageRequestDto request)
        {
            var pageResult = await _activityRepository.AsNoTracking()
                .Where(m => m.IsClub && m.ClubId == request.ClubId)
                .OrderByDescending(m => m.CreateTime).PageListAsync<Activity, ClubActivityDto>(request, _mapper);
            return pageResult;
        }
    }
}
